using System; 
using System.Collections.Generic;
using UnityEngine;

namespace XFGameFramework
{
    internal class ControllerManager
    {
        internal static void AddController(Module module, Controller controller)
        {

            Type type = controller.GetType();

            if (!module.controllers.ContainsKey(type))
            {
                controller.Module = module;
                controller.OnInit();
                module.controllers.Add(type, controller);
            }
            else
            {
                Debug.LogWarningFormat("controller name:{0} 已经存在,请勿重复添加!", type);
            }
        }

        internal static void RemoveController(Module module, Type controllerType)
        {
            if (!module.controllers.ContainsKey(controllerType))
                return;

            Controller controller = module.controllers[controllerType];

            //if (controller.Type == ControllerType.CannotRemove)
            //    return;

            controller.OnExit();
            module.controllers.Remove(controllerType); 
        }

        internal static Controller GetController(Module module, Type controllerType)
        {

            Controller controller;
            if (module.controllers.TryGetValue(controllerType, out controller))
            {
                return controller;
            }

            
            // 尝试 从基础其他模块中( Controller 只需要有一个 不需要有多个 )
            foreach (var item in ModuleManager.modules.Values)
            {

                if (item == module) continue;

                if (item.controllers.ContainsKey(controllerType))
                    return item.controllers[controllerType];
            }


            return null;
        }

        internal static Controller LoadController(Module module, Type controllerType)
        {

            Controller controller = GetController(module, controllerType);
            if (controller != null)
            {
                return controller;
            }
            Type type = controllerType;

            if (type == null)
            {
                Debug.LogErrorFormat("加载Controller:{0}失败!未查询到对应类型", controllerType);
                return null;
            }

            // 创建一个业务逻辑
            controller = System.Activator.CreateInstance(type) as Controller;
            if (controller == null)
            {
                Debug.LogErrorFormat("创建Controller:{0}失败! 请检查类名是否正确!如果有命名空间请加上!", controllerType);
                return null;
            }
            controller.Module = module;
            AddController(module,controller);

            return controller;
        }

        internal static T LoadController<T>(Module module) where T : Controller
        {
            T controller = GetController(module,typeof(T)) as T;
            if (controller != null) return controller;

            // 创建一个业务逻辑
            controller = Activator.CreateInstance<T>();
            controller.Module = module;
            AddController(module, controller) ;
            return controller;
        }


        internal static void ClearController(Module module)
        {
            List<Type> removes = new List<Type>();

            foreach (var item in module.controllers.Keys)
            {
                removes.Add(item);
            }

            foreach (var item in removes)
            {  
                module.RemoveController(item);
            }

            module.controllers.Clear();
        }

    }

}